home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / hmac.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  4KB  |  122 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''HMAC (Keyed-Hashing for Message Authentication) Python module.
  5.  
  6. Implements the HMAC algorithm as described by RFC 2104.
  7. '''
  8. import warnings as _warnings
  9. trans_5C = ''.join([ chr(x ^ 92) for x in xrange(256) ])
  10. trans_36 = ''.join([ chr(x ^ 54) for x in xrange(256) ])
  11. digest_size = None
  12. _secret_backdoor_key = []
  13.  
  14. class HMAC:
  15.     '''RFC 2104 HMAC class.  Also complies with RFC 4231.
  16.  
  17.     This supports the API for Cryptographic Hash Functions (PEP 247).
  18.     '''
  19.     blocksize = 64
  20.     
  21.     def __init__(self, key, msg = None, digestmod = None):
  22.         '''Create a new HMAC object.
  23.  
  24.         key:       key for the keyed hash object.
  25.         msg:       Initial input for the hash, if provided.
  26.         digestmod: A module supporting PEP 247.  *OR*
  27.                    A hashlib constructor returning a new hash object.
  28.                    Defaults to hashlib.md5.
  29.         '''
  30.         if key is _secret_backdoor_key:
  31.             return None
  32.         if None is None:
  33.             import hashlib as hashlib
  34.             digestmod = hashlib.md5
  35.         if hasattr(digestmod, '__call__'):
  36.             self.digest_cons = digestmod
  37.         else:
  38.             
  39.             self.digest_cons = lambda d = ('',): digestmod.new(d)
  40.         self.outer = self.digest_cons()
  41.         self.inner = self.digest_cons()
  42.         self.digest_size = self.inner.digest_size
  43.         if hasattr(self.inner, 'block_size'):
  44.             blocksize = self.inner.block_size
  45.             if blocksize < 16:
  46.                 _warnings.warn('block_size of %d seems too small; using our default of %d.' % (blocksize, self.blocksize), RuntimeWarning, 2)
  47.                 blocksize = self.blocksize
  48.             
  49.         else:
  50.             _warnings.warn('No block_size attribute on given digest object; Assuming %d.' % self.blocksize, RuntimeWarning, 2)
  51.             blocksize = self.blocksize
  52.         if len(key) > blocksize:
  53.             key = self.digest_cons(key).digest()
  54.         key = key + chr(0) * (blocksize - len(key))
  55.         self.outer.update(key.translate(trans_5C))
  56.         self.inner.update(key.translate(trans_36))
  57.         if msg is not None:
  58.             self.update(msg)
  59.  
  60.     
  61.     def update(self, msg):
  62.         '''Update this hashing object with the string msg.
  63.         '''
  64.         self.inner.update(msg)
  65.  
  66.     
  67.     def copy(self):
  68.         """Return a separate copy of this hashing object.
  69.  
  70.         An update to this copy won't affect the original object.
  71.         """
  72.         other = self.__class__(_secret_backdoor_key)
  73.         other.digest_cons = self.digest_cons
  74.         other.digest_size = self.digest_size
  75.         other.inner = self.inner.copy()
  76.         other.outer = self.outer.copy()
  77.         return other
  78.  
  79.     
  80.     def _current(self):
  81.         '''Return a hash object for the current state.
  82.  
  83.         To be used only internally with digest() and hexdigest().
  84.         '''
  85.         h = self.outer.copy()
  86.         h.update(self.inner.digest())
  87.         return h
  88.  
  89.     
  90.     def digest(self):
  91.         '''Return the hash value of this hashing object.
  92.  
  93.         This returns a string containing 8-bit data.  The object is
  94.         not altered in any way by this function; you can continue
  95.         updating the object after calling this function.
  96.         '''
  97.         h = self._current()
  98.         return h.digest()
  99.  
  100.     
  101.     def hexdigest(self):
  102.         '''Like digest(), but returns a string of hexadecimal digits instead.
  103.         '''
  104.         h = self._current()
  105.         return h.hexdigest()
  106.  
  107.  
  108.  
  109. def new(key, msg = None, digestmod = None):
  110.     """Create a new hashing object and return it.
  111.  
  112.     key: The starting key for the hash.
  113.     msg: if available, will immediately be hashed into the object's starting
  114.     state.
  115.  
  116.     You can now feed arbitrary strings into the object using its update()
  117.     method, and can ask for the hash value at any time by calling its digest()
  118.     method.
  119.     """
  120.     return HMAC(key, msg, digestmod)
  121.  
  122.